package com.example.aigc_education.ws.handler;

import com.example.aigc_education.rpc.api.v2.ChatServiceProto;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class MessageHandlerFactory {

    private static final List<MessageHandler> HANDLERS = new ArrayList<>();

    static {
        // 注册所有的处理器
        HANDLERS.add(new ThinkingMessageHandler());
        HANDLERS.add(new TextMessageHandler());
        HANDLERS.add(new SingleSelectMessageHandler());
        HANDLERS.add(new CollapseStartMessageHandler());
        HANDLERS.add(new CollapseEndMessageHandler());
        HANDLERS.add(new MapMessageHandler());
        HANDLERS.add(new MapMessageHandler());
        HANDLERS.add(new UserTestMessageHandler());
//        handlers.add(new ImageUrlMessageHandler());
//        handlers.add(new InputAudioMessageHandler());
//        handlers.add(new VideoUrlMessageHandler());

    }

    public static MessageHandler getHandler(ChatServiceProto.ChatMessage chatMessage) {
        for (MessageHandler handler : HANDLERS) {
            if (handler.canHandle(chatMessage)) {
                return handler;
            }
        }

        log.warn("[MessageHandlerFactory] getHandler No handler found for message type: {}, role: {}",
                chatMessage.getContentsCount() > 0 ? chatMessage.getContents(0).getType() : "unknown",
                chatMessage.getRole());

        return new DefaultMessageHandler();
    }
}
